<template>
	<view class="wrap">
		<view class="filter-box">
			<view class="b-item" v-for="(item, index) in groupList" :key="index"
				:class="{'f-active': index == groupIndex}" @click="onGroupSelect(index)">
				<view class="s-tit">{{item.name}}</view>
				<view class="iconfont" v-if="item.sort && index == groupIndex"
					:class="{'icon-unfold': item.sort == 'asc','icon-fold': item.sort == 'desc'}"></view>
			</view>
		</view>
		<view class="gift-list">
			<view class="b-items" v-if="state.items.length > 0">
				<view class="b-item" v-for="(item, index) in state.items" :key="index" @click="onJumpGift(item.id)">
					<view class="b-left">
						<image :src="item.cover_url" mode="aspectFill"></image>
					</view>
					<view class="b-main">
						<view class="b-title">{{item.name}}</view>
						<view class="b-center">
							<view class="b-fund">
								<view class="s-num">{{item.amount}}礼币</view>
								<view class="s-worth" v-if="item.worth > 0">价值 {{item.worth}}元</view>
							</view>
							<view class="b-action">
								<view class="s-action" v-if="item.count_left > 0">兑换</view>
								<view class="s-action f-disable" v-else>已兑完</view>
							</view>
						</view>
						<view class="b-amount">
							<view class="s-left">库存 {{item.count_left}}</view>
							<view class="s-gain">已兑 {{item.count_gain}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
	</view>
</template>

<script>
	import {
		getList
	} from '@/service/api/gift.js'
	import mixinsCommon from '@/mixins/common.js'
	import mixinsAuth from '@/mixins/auth.js'
	export default {
		mixins: [mixinsCommon, mixinsAuth],
		data() {
			return {
				groupIndex: 0,
				groupList: [{
						name: '全部',
						value: 'all',
						sort: ''
					},
					{
						name: '按兑换量',
						value: 'gain',
						sort: 'desc'
					},
					{
						name: '按礼币数',
						value: 'amount',
						sort: 'asc'
					},
				],
				state: {
					items: []
				},
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadList();
			}
		},
		methods: {
			loadList(refresh) {
				console.log('loadList', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getList(this.groupList[this.groupIndex].value, this.groupList[this.groupIndex].sort, this.page, 10).then(([
					err, res
				]) => {
					console.log('getList', err, res)
					this.loadingType = -1;
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onGroupSelect(index) {
				if (index == this.groupIndex) {
					const sort = this.groupList[this.groupIndex].sort
					if (sort) {
						if (sort === 'asc') {
							this.groupList[this.groupIndex].sort = 'desc'
						} else {
							this.groupList[this.groupIndex].sort = 'asc'
						}
					}
				}
				this.groupIndex = index
				this.loadList(true)
			},
			onJumpGift(giftId) {
				uni.navigateTo({
					url: '/pages/gift/detail?gift_id=' + giftId
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}

	.filter-box {
		padding: 30upx;
		background: #fff;
		display: flex;
		align-items: center;

		.b-item {
			width: 240upx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 30upx;

			&.f-active {
				font-weight: bold;
			}

			.s-tit {
				color: #333;
			}

			.iconfont {
				color: #999;
				margin-left: 10upx;
			}
		}
	}

	.gift-list {
		background: #fff;

		.b-items {
			.b-item {
				padding: 30upx;
				border-bottom: 1upx solid #eee;
				display: flex;

				&:last-child {
					border-bottom: none;
				}

				.b-left {
					width: 160upx;
					height: 160upx;

					image {
						width: 160upx;
						height: 160upx;
					}
				}

				.b-main {
					margin-left: 30upx;
					flex: 1;
					display: flex;
					flex-direction: column;

					.b-title {
						font-size: 28upx;
						color: #333;
					}

					.b-center {
						margin-top: 20upx;
						display: flex;
						justify-content: space-between;

						.b-fund {
							display: flex;
							align-items: baseline;

							.s-num {
								font-size: 32upx;
								color: #DA5650;
							}

							.s-worth {
								margin-left: 30upx;
								font-size: 24upx;
								color: #999;
							}
						}

						.b-action {
							.s-action {
								background: #DA5650;
								color: #fff;
								font-size: 24upx;
								padding: 0 30upx;
								border-radius: 30upx;
								height: 60upx;
								line-height: 60upx;

								&.f-disable {
									background: rgba(218, 86, 80, 0.8)
								}
							}
						}
					}

					.b-amount {
						margin-top: 10upx;
						display: flex;
						justify-content: space-between;

						.s-left {
							font-size: 24upx;
							color: #999;
						}

						.s-gain {
							font-size: 24upx;
							color: #999;
						}
					}
				}
			}
		}
	}
</style>
